#############################################
###### Replication Code for Figure A10 ######
########### January 9th, 2025 ###############
#############################################

rm(list=ls())

### Script produces Figure A10 and saves it in the `/figures` folder

library(fixest)
library(modelsummary)
library(ggplot2)
df <- readRDS("data/workfile.rds")

###### Treatments effects by condition
df <- df %>% 
  group_by(dem_country_code) %>% 
  mutate(trust_in_opposing_parties2 = scale(trust_in_opposing_parties))
df$trust_in_opposing_parties <- scale(df$trust_in_opposing_parties)

df <- df %>% filter(!is.na(in_party_feeling))

coef_df <- data.frame()

trust_no_cov <- feols(trust_in_opposing_parties ~ treated_econ + treated_culture, df, se = "iid") %>% 
  summary()
trust_cts_fe <- feols(trust_in_opposing_parties ~ treated_econ + treated_culture  | dem_country_code, df, se = "iid") %>% 
  summary()
trust_cov_adj <- feols(trust_in_opposing_parties ~ treated_econ + treated_culture + 
                         mean_outparty_feeling + high_pol_interest + in_party_feeling | dem_country_code, df, se = "iid") %>% 
  summary()

coef_df <- data.frame(coef = c(trust_no_cov$coefficients[2:3], trust_cts_fe$coefficients[1:2], trust_cov_adj$coefficients[1:2]), 
                      se = c(trust_no_cov$se[2:3], trust_cts_fe$se[1:2], trust_cov_adj$se[1:2]),
                      name = rep(c("Economic issues \n condition", "Cultural issues \n condition"), times = 3),
                      var = rep(c("No covariates", "Country-FE", "Country-FE + covariate adj."), each = 2))

pd <- position_dodge(0.5)

coef_df <- coef_df

regs <- ggplot(coef_df, aes(x = name,y = coef, color = var)) + 
  ylim(c(-0.15, 0.05)) +
  geom_point(position = pd, shape = 21, fill = 'white') + 
  geom_errorbar(aes( ymin = coef - se*1.959964, ymax = coef + se*1.959964), width = 0, position = pd) + 
  geom_errorbar(aes( ymin = coef - se*1.644854, ymax = coef + se*1.644854), width = 0, position = pd, linewidth = 1.5) + 
  geom_hline(yintercept = 0, color = "red", linetype = "dashed") +
  scale_colour_manual(values = c("black", "grey", "darkorange")) + 
  xlab("") + 
  ylab("Treatment effect on out-partisan trust") +
  coord_flip() +
  #facet_wrap(~ var) + 
  theme_bw() + 
  guides(color=guide_legend(title=""))
regs

ggsave("figures/FigA10_agg_treatments_partisans.pdf", 
       height = 4, width = 6)


